package myaction.student;

import myutil.DbUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class StudentSelectCourseAction extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        String id = request.getParameter("id");
        String c_id = null;
        String c_name = null;
        String c_time = null;
        String c_teacher_id = null;
        String c_teacher_name = null;
        String s_id = String.valueOf(session.getAttribute("student_id"));
        String s_name = String.valueOf(session.getAttribute("student_name"));
//      更新t_courses表中c_isable字段的值
        try {
            Connection conn = DbUtil.getConn();
            String sql1 = "update t_courses set c_isable='0' where  c_id=?";
            PreparedStatement ps = conn.prepareStatement(sql1);
            ps.setInt(1, Integer.parseInt(id));
            ps.executeUpdate();
//      获取t_courses表的字段,放在变量中
            String sql2 = "select c_id, c_name, DATE(c_time), c_teacher_id, t_name from t_courses left join t_teachers on t_courses.c_teacher_id=t_teachers.t_id where c_id=?";
            String sql2_id = request.getParameter("id");
            PreparedStatement ps2 = conn.prepareStatement(sql2);
            ps2.setString(1, sql2_id);
            ResultSet rs = ps2.executeQuery();
            if (rs.next()) {
                c_id = String.valueOf(rs.getInt("c_id"));
                c_name = rs.getString("c_name");
                c_time = rs.getString("Date(c_time)");
                c_teacher_id = rs.getString("c_teacher_id");
                c_teacher_name = rs.getString("t_name");
            }
            System.out.println("课程" + c_id + "已被选择!");
//      插入t_opt表中一条记录
            String sql3 = "insert into t_opt(o_id,o_name,o_time,o_student_id,o_student_name,c_teacher_id,c_teacher_name) " +
                    "values(?,?,?,?,?,?,?)";
            PreparedStatement ps3 = conn.prepareStatement(sql3);
            ps3.setString(1, c_id);
            ps3.setString(2, c_name);
            ps3.setString(3, c_time);
            ps3.setString(4, s_id);
            ps3.setString(5, s_name);
            ps3.setString(6, c_teacher_id);
            ps3.setString(7, c_teacher_name);
            int result = ps3.executeUpdate();
            ps.close();
            rs.close();
            ps2.close();
            ps3.close();
            conn.close();
            System.out.println("StudentSelectCourseAction");
        } catch (Exception e) {
            e.printStackTrace();
        }
        response.sendRedirect("StudentCoursesFindAll");
    }
}
